#include<iostream>
using namespace std;
#include<queue>
queue<int> que;
int step[100005]; 
bool vis[100005]; 
int main() {
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		step[i] = 0;
		vis[i] = false;
	}
	que.push(0);
	step[0] = 0;
	vis[0] = true;
	while (!que.empty()) {
		int now = que.front();
		que.pop();
		int one_step = (now + 1) % n;
		if (vis[one_step] == false) {
			vis[one_step] = true;
			step[one_step] = step[now] + 1;
			que.push(one_step);
		}
		int k_step = (now + k) % n;
		if (vis[k_step] == false) {
			vis[k_step] = true;
			step[k_step] = step[now] + 1;
			que.push(k_step);
		}
	}
	int max_step = step[0];
	for (int i = 0; i < n; i++) {
		max_step = max(max_step, step[i]);
	}
	cout << max_step << endl;
	return 0;
}

